/*
 * BString.h
 *
 *  Created on: Oct 11, 2011
 *      Author: eran
 *      This BString class simply wraps a string while maintaining performance and simplicity
 */

#ifndef _BSTRING_HEADER_
#define _BSTRING_HEADER_
#include <string.h>
#include <string>
#include <vector>
#include "BUtilities.h"

using namespace std;
#define B_INIT_STRING_SIZE			20 //Initial string size
#define B_STRING_EXPANSION_SIZE		10 //Default expansion size. The string will be expanded by multiples of this size
namespace blueberry
{
class BString;
typedef vector<BString*> BSTRING_VECTOR;
class BStringTokenList
{
public:
	BStringTokenList(){ite = vec_Tokens.begin();}
	~BStringTokenList();
	BString * getNextToken();
	inline void reset(){ite = vec_Tokens.begin();}
	friend class BString;
private:
	BSTRING_VECTOR vec_Tokens;
	BSTRING_VECTOR::iterator ite;
};
class BString
{
public:
	//Constructors
	BString();//Creates am empty string
	BString(BString & _rSrc);//Copy constructor
	BString(const char * _zData);//Copy contents pointed by _zData. Null termination is assumed and required
	BString(string & _rSrc);//Copy contents of _sSrc
	BString(const char * _zBuffer, UInt _uiLen);

	//Distructor
	~BString();

	//Appenders
	void append(const char * _zData);//Null termination is assumed and required
	void append(BString & _rSrc);
	void append(string & _rSrc);

	//Operators
	//Appending operators
	void operator+=(const char * _zData);//Null termination is assumed and required
	void operator+=(BString & _rSrc);
	void operator+=(string & _rSrc);
	void operator+=(int _iValue);
	void operator+=(UInt _uiValue);
	void operator+=(float _fValue);
	void operator+=(double _dValue);
	void operator+=(char _cValue);
	void operator+=(Byte _btValue);


	//Utilities
	void resize(UInt _uiSize);
	void trim();
	void trim(char _cDelim);
	BStringTokenList * tokenize(char _cDelim);//Tokenize the string and BStringTokenList should be deleted by the caller
	int find(const char * _zStr);//Returns the position of text found, -1 for nothing

	//Getters
    inline UInt getUi_Len() const { return ui_Len;}//Returns Length without null charector

    //Printers
    const char * c_str()const {return z_Data;}

private:
	char * z_Data;
	UInt ui_Len;//Length without null charector
	UInt ui_AllocatedLen;//Number of space available for this string in the memory
};
}

#endif /* BSTRING_H_ */
